home *** CD-ROM | disk | FTP | other *** search
/ Aminet 1 (Walnut Creek) / Aminet - June 1993 [Walnut Creek].iso / aminet / util / gnu / fileutils_3_3.lha / fileutils-3.3 / lib / eaccess.c < prev    next >
C/C++ Source or Header  |  1992-08-01  |  4KB  |  172 lines

  1. /* eaccess -- check if effective user id can access file
  2.    Copyright (C) 1990, 1991 Free Software Foundation, Inc.
  3.  
  4.    This program is free software; you can redistribute it and/or modify
  5.    it under the terms of the GNU General Public License as published by
  6.    the Free Software Foundation; either version 2, or (at your option)
  7.    any later version.
  8.  
  9.    This program is distributed in the hope that it will be useful,
  10.    but WITHOUT ANY WARRANTY; without even the implied warranty of
  11.    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  12.    GNU General Public License for more details.
  13.  
  14.    You should have received a copy of the GNU General Public License
  15.    along with this program; if not, write to the Free Software
  16.    Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.  */
  17.  
  18. /* Written by David MacKenzie and Torbjorn Granlund. */
  19.  
  20. #include <sys/types.h>
  21. #include <sys/stat.h>
  22.  
  23. #ifdef HAVE_UNISTD_H
  24. #include <unistd.h>
  25. #endif
  26.  
  27. #ifdef _POSIX_VERSION
  28. #include <limits.h>
  29. #ifdef NGROUPS_MAX
  30. #undef NGROUPS_MAX
  31. #endif /* NGROUPS_MAX */
  32. #define NGROUPS_MAX sysconf (_SC_NGROUPS_MAX)
  33. #if !defined(sun) && !defined(ultrix)
  34. #define GETGROUPS_T gid_t
  35. #else /* sun or ultrix */
  36. #define GETGROUPS_T int
  37. #endif /* sun or ultrix */
  38. #else /* not _POSIX_VERSION */
  39. uid_t geteuid ();
  40. gid_t getegid ();
  41. #include <sys/param.h>
  42. #if !defined(NGROUPS_MAX) && defined(NGROUPS)
  43. #define NGROUPS_MAX NGROUPS
  44. #endif /* not NGROUPS_MAX and NGROUPS */
  45. #define GETGROUPS_T int
  46. #endif /* POSIX_VERSION */
  47.  
  48. #include <errno.h>
  49. #ifndef STDC_HEADERS
  50. extern int errno;
  51. #endif
  52.  
  53. #if defined(EACCES) && !defined(EACCESS)
  54. #define EACCESS EACCES
  55. #endif
  56.  
  57. #ifndef F_OK
  58. #define F_OK 0
  59. #define X_OK 1
  60. #define W_OK 2
  61. #define R_OK 4
  62. #endif
  63.  
  64. int eaccess_stat ();
  65.  
  66. /* The user's effective user id. */
  67. static uid_t euid;
  68.  
  69. /* The user's effective group id. */
  70. static gid_t egid;
  71.  
  72. #ifdef NGROUPS_MAX
  73. char *xmalloc ();
  74. static int in_group ();
  75.  
  76. /* Array of group id's that the user is in. */
  77. static GETGROUPS_T *groups = 0;
  78.  
  79. /* The number of valid elements in `groups'. */
  80. static int ngroups;
  81. #endif
  82.  
  83. /* Nonzero if the other static variables have valid values. */
  84. static int initialized = 0;
  85.  
  86. /* Return 0 if the user has permission of type MODE on file PATH;
  87.    otherwise, return -1 and set `errno' to EACCESS.
  88.    Like access, except that it uses the effective user and group
  89.    id's instead of the real ones, and it does not check for read-only
  90.    filesystem, text busy, etc. */
  91.  
  92. int
  93. eaccess (path, mode)
  94.      char *path;
  95.      int mode;
  96. {
  97.   struct stat stats;
  98.  
  99.   if (stat (path, &stats))
  100.     return -1;
  101.  
  102.   return eaccess_stat (&stats, mode);
  103. }
  104.  
  105. /* Like eaccess, except that a pointer to a filled-in stat structure
  106.    describing the file is provided instead of a filename. */
  107.  
  108. int
  109. eaccess_stat (statp, mode)
  110.      struct stat *statp;
  111.      int mode;
  112. {
  113.   int granted;
  114.  
  115.   mode &= (X_OK | W_OK | R_OK);    /* Clear any bogus bits. */
  116.  
  117.   if (mode == F_OK)
  118.     return 0;            /* The file exists. */
  119.  
  120.   if (initialized == 0)
  121.     {
  122.       initialized = 1;
  123.       euid = geteuid ();
  124.       egid = getegid ();
  125. #ifdef NGROUPS_MAX
  126.       groups = (GETGROUPS_T *) xmalloc (NGROUPS_MAX * sizeof (GETGROUPS_T));
  127.       ngroups = getgroups (NGROUPS_MAX, groups);
  128. #endif
  129.     }
  130.   
  131.   /* The super-user can read and write any file, and execute any file
  132.      that anyone can execute. */
  133.   if (euid == 0 && ((mode & X_OK) == 0 || (statp->st_mode & 0111)))
  134.     return 0;
  135.   if (euid == statp->st_uid)
  136.     granted = (statp->st_mode & (mode << 6)) >> 6;
  137.   else if (egid == statp->st_gid
  138. #ifdef NGROUPS_MAX
  139.        || in_group (statp->st_gid)
  140. #endif
  141.        )
  142.     granted = (statp->st_mode & (mode << 3)) >> 3;
  143.   else
  144.     granted = (statp->st_mode & mode);
  145.   if (granted == mode)
  146.     return 0;
  147.   errno = EACCESS;
  148.   return -1;
  149. }
  150.  
  151. #ifdef NGROUPS_MAX
  152. static int
  153. in_group (gid)
  154.      GETGROUPS_T gid;
  155. {
  156.   int i;
  157.  
  158.   for (i = 0; i < ngroups; i++)
  159.     if (gid == groups[i])
  160.       return 1;
  161.   return 0;
  162. }
  163. #endif
  164.  
  165. #ifdef TEST
  166. main (argc, argv)
  167.      char **argv;
  168. {
  169.   printf ("%d\n", eaccess (argv[1], atoi (argv[2])));
  170. }
  171. #endif
  172.